//index.js
//获取应用实例
var md5 = require("../../utils/md5.js");
var app = getApp()
Page({
	data: {
		indicatorDots: true,
		autoplay: true,
		interval: 3000,
		duration: 1000,
		loadingHidden: false , // loading
    width: wx.getStorageSync('width'),
    height: wx.getStorageSync('height'),
		userInfo: {},
    	swiperCurrent: 0,  
    	selectCurrent:0,
    	categories: [],
    	activeCategoryId: 0,
    	goods:[],
    	scrollTop:"0",
    	loadingMoreHidden:true,
    	page: 1,//加载商品时的页数默认为1开始,在app页面加载
    	page_count: 10,//每页商品数设置为10确保能全部加载商品，在app页面加载
    	hasNoCoupons:true,
    	coupons: [],
      goods_len:0
	},
	//事件处理函数
	toDetailsTap:function(e){//商品明细页面
		wx.navigateTo({
			url:"/pages/goods-details/index?id="+e.currentTarget.dataset.id
		})
	},
	onLoad: function () {//页面加载
		var that = this
	    wx.setNavigationBarTitle({
	    	title: wx.getStorageSync('mall_name')
	    })
	    that.setData({
	    	categories: [{id:0, name:"热销商品"}],
	    	activeCategoryId: 0,
	    	goods: [],
	    	page: 1,
        open_id: app.globalData.openId,
        user_id: app.globalData.userId,
	    });
		that.getGoodsList(0);
	},
	getGoodsList: function (categoryId) {
	    if (categoryId == 0) {
	      categoryId = "";
	    }
	    var that = this;
      var service = 'Product.GetShopGongxiang';
      var shop_id = app.globalData.shopId
	    var page = this.data.page;
	    var page_count = this.data.page_count;
      var sign = md5.md5(page + ''+ page_count+'' + service + shop_id);
	    wx.request({
	      url: app.globalData.serviceUrl + 'Product.GetShopGongxiang',
	      data: {
	    	  sign: sign,
          shop_id: shop_id,
	    	  page: page,
	    	  page_count: page_count,
	      },
	      header: {
	    	  'Content-Type': 'json'
	      },
	      success: function(res) {
	    	  if (res.data.data.code == 0) {
            if (res.data.data.info.length>0){
              var goods = that.data.goods;
              for (var i = 0; i < res.data.data.info.length; i++) {
                goods.push(res.data.data.info[i]);
              }
              that.setData({
                goods: goods,
                goods_len: goods.length
              });
            }else{
              wx.showToast({
                title: '暂无更多商品',
              })
            }
		          
	    	  }
	      },
	      complete: function(res) {
	    	  console.log(res);
	      },
	    })
	},
	onReachBottom: function(){//上划加载
		this.data.page += 1;
		this.getGoodsList(0);
	},
	onPullDownRefresh: function () {//下划加载
		this.setData({
			loadingMore: true,
			isEnd: false,
      page:1,
      goods:[]
		});
		wx.showNavigationBarLoading();
    this.getGoodsList(0);
		wx.hideNavigationBarLoading();//完成停止加载
		wx.stopPullDownRefresh();//停止下拉刷新
	},

  //分享小程序
  onShareAppMessage: function () {
    return {
      title: app.globalData.shopName,
      path: '/pages/start/start?shop_id=' + app.globalData.shopId,
      imageUrl: app.globalData.shopPic,
    }
  },
})
